package com.zygh.policeasset.controller;

import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.component.OperLog;
import com.zygh.policeasset.pojo.ZipOrRar;
import com.zygh.policeasset.service.AssetsFilesService;
import com.zygh.policeasset.utils.UnZipAndRarUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.util.List;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping({"/newAssetsfiles"})
@Api(tags = {"(新)资产模块上传数据压缩包"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/NewAssetsFilesController.class */
public class NewAssetsFilesController {
    @Autowired
    AssetsFilesService assetsFilesService;
    @Value("${file.uploadFolderzn}")
    private String uploadFolderzn;
    @Value("${file.uploadFolderjc}")
    private String uploadFolderjc;
    @Value("${file.uploadFolderda}")
    private String uploadFolderda;
    @Value("${file.uploadFoldercg}")
    private String uploadFoldercg;

    @RequestMapping(value = {"/addRepository"}, method = {RequestMethod.POST})
    @ApiOperation("上传数据压缩包")
    @Transactional(rollbackFor = {Exception.class})
    @OperLog(operModul = "资产模块上传数据压缩包", operType = "添加", operDesc = "资产模块上传数据压缩包")
    public Object addRepository(@RequestBody List<MultipartFile> files, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "projectNo", required = false) String projectNo, @RequestParam(value = "inputNum", required = false) String inputNum, @RequestParam(value = "sectionName", required = false) String sectionName, @RequestParam(value = "sectionId", required = false) Integer sectionId, @RequestParam(value = "companyId", required = false) Integer companyId, @RequestParam(value = "selfBuilt", required = false) Integer selfBuilt, @RequestParam(value = "projectType", required = false) String projectType) {
        String outDir;
        File path = null;
        try {
            if (!files.isEmpty() && files.size() > 0) {
                for (MultipartFile multipartFile : files) {
                    if ("zn".equals(type)) {
                        outDir = this.uploadFolderzn + System.currentTimeMillis() + "/";
                    } else if ("jc".equals(type)) {
                        outDir = this.uploadFolderjc + System.currentTimeMillis() + "/";
                    } else if ("da".equals(type)) {
                        outDir = this.uploadFolderda + System.currentTimeMillis() + "/";
                    } else if (!"cg".equals(type)) {
                        return new CommonResult().validateFailed("资产类型错误");
                    } else {
                        outDir = this.uploadFoldercg + System.currentTimeMillis() + "/";
                    }
                    File outFile = new File(outDir);
                    if (!outFile.exists()) {
                        outFile.mkdirs();
                    }
                    String fileName = multipartFile.getOriginalFilename();
                    String filePath = outDir + fileName;
                    System.out.println(filePath);
                    multipartFile.transferTo(new File(filePath));
                    if (!fileName.substring(fileName.lastIndexOf(".")).equals(".zip")) {
                        return new CommonResult().validateFailed("上传文件类型错误");
                    }
                    ZipOrRar zipOrRar = UnZipAndRarUtil.unZip(filePath, outDir);
                    path = new File(outDir);
                    if ("zn".equals(type)) {
                        this.assetsFilesService.deZnCompress(zipOrRar, projectNo, inputNum, sectionName, sectionId, companyId, type, selfBuilt);
                    } else if ("jc".equals(type)) {
                        this.assetsFilesService.deJcCompress(zipOrRar, projectNo, inputNum, sectionName, sectionId, companyId, type, selfBuilt);
                    } else if ("da".equals(type)) {
                        this.assetsFilesService.deDaCompress(zipOrRar, projectNo, inputNum, companyId, type, selfBuilt, projectType);
                    } else if ("cg".equals(type)) {
                        this.assetsFilesService.deCgCompress(zipOrRar, projectNo, inputNum, type, selfBuilt);
                    }
                }
            }
            UnZipAndRarUtil.delete(path);
            return new CommonResult().success();
        } catch (ExcelAnalysisException e) {
            UnZipAndRarUtil.delete(null);
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            if (!(e.getCause() instanceof ExcelDataConvertException)) {
                return new CommonResult().validateFailed(e.toString().split("-")[1]);
            }
            ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) e.getCause();
            String cellMsg = "";
            CellData cellData = excelDataConvertException.getCellData();
            CellDataTypeEnum type1 = cellData.getType();
            if (type1.equals(CellDataTypeEnum.NUMBER)) {
                cellMsg = cellData.getNumberValue().toString();
            } else if (type1.equals(CellDataTypeEnum.STRING)) {
                cellMsg = cellData.getStringValue();
            } else if (type1.equals(CellDataTypeEnum.BOOLEAN)) {
                cellMsg = cellData.getBooleanValue().toString();
            }
            return new CommonResult().validateFailed(String.format("第%s行,第%s列,数据值为:%s,该数据值不符合要求,请检验后重新导入!", Integer.valueOf(excelDataConvertException.getRowIndex().intValue() + 1), Integer.valueOf(excelDataConvertException.getColumnIndex().intValue() + 1), cellMsg));
        } catch (ConstraintViolationException e2) {
            UnZipAndRarUtil.delete(null);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e2.printStackTrace();
            return new CommonResult().validateFailed(e2.toString());
        } catch (Exception e3) {
            UnZipAndRarUtil.delete(null);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e3.printStackTrace();
            return new CommonResult().validateFailed(e3.toString());
        }
    }
}